package com.javathread.thread01;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.javathread.aqs.TrinityLock;

public class TestMyLock {
	public void test() {
		final Lock lock = new TrinityLock();
		class Worker extends Thread{
			@Override
			public void run() {
				while (true) {
					lock.lock();
					try {
						SleepTools.second(1);
						System.out.println("sub:"+Thread.currentThread().getName());
						SleepTools.second(1);
					} finally {
						lock.unlock();
					}
					SleepTools.second(2);
				}
			}
			
		}
		
		//启动10个子线程
		for (int i =0; i < 10; i++) {
			Worker w = new Worker();
			w.setDaemon(true);
			w.start();
		}
		//主线程每隔1秒换行
		for (int i =0; i < 60 ; i++) {
			SleepTools.second(1);
			System.out.println("main*");
		}
	}
	
	public static void main(String[] args) {
		TestMyLock testMyLock = new TestMyLock();
		testMyLock.test();
	}
}
